<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseAdminController.php';
require_once 'CMS/AdminCommon.php';

//Người tạo : Lại Ngọc Thái Châu - Team 0
//Module 	: ListuserManager
//Chức năng	: Quản lý các nhóm user

class Admin_UserController extends BaseAdminController {
	public function init(){
		parent::init();
		$this->_helper->layout()->setLayout('layoutadmin');
		$user = new User();
		$user->init();
		$user->check_login();
		$user->permission();
	}
	
	public function indexAction(){
		$session = new Zend_Session_Namespace('login.auth');
		if(isset($session->delete_user) && $session->delete_user!="" )
		{
			$this->view->success = $session->delete_user;
			unset($session->delete_user);
		}
		else if(isset($session->update_user) && $session->update_user!="" )
		{
			$this->view->success = $session->update_user;
			unset($session->update_user);
		}
		else if(isset($session->insert_user) && $session->insert_user!="" )
		{
			$this->view->success = $session->insert_user;
			unset($session->insert_user);
		}
		$tbUser = new Admin_Model_Users();
		$date = new Util();
		$data_user = $tbUser->selectALLUser();
		
		$i=0;
		foreach($data_user as $da_us)
		{
			$array[$i] = array(
			'id' => $da_us['user_id'],
			'name' => $da_us['user_name'],
			'status' => $da_us['status']==1 ? "Enable" : "Disable" ,
			'date_created' => $date->makeDays($da_us['created_date']),
			);
			$i++;
		}
		$this->view->data_user = $array;
	}
	public function insertAction(){
		$date = new Util();
		$session = new Zend_Session_Namespace('login.auth');
		$tbUserGroup = new Admin_Model_GroupUser();
		$this->view->group_user = $tbUserGroup->selectALLUserGroup();
		$tbUser = new Admin_Model_Users();
		if (!isset($_GET['id'])) {
			$this->view->action = "/admin/user/insert";
			$this->view->button = "Insert";
		} else {
			$this->view->action = "/admin/user/insert?id=".$_GET['id'];
			$this->view->button = "Update";
		}
		  
    	$this->view->cancel ="/admin/user";
		if (isset($_POST) && $_POST!=null) {
				$warning="";
				$pattern = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
				
				if(!isset($_POST['username'])||$_POST['username']=="" || $_POST['username']==null)
				{
					$warning .="You did not enter User Name<br>";
				}
				else 
				{
					if(isset($_GET['id']) && is_numeric($_GET['id']))
					{
						$temp = $tbUser->selectUserByUserName($_POST['username'],$_GET['id']);
					}
					else 
					{
						$temp = $tbUser->selectUserByUserName($_POST['username'],0);
					}
					if(count($temp)>0)
					{
						$warning .="User Name was in use<br>";
					}
				}
				if(!isset($_POST['fullname'])||$_POST['fullname']=="" || $_POST['fullname']==null)
				{
					$warning .="You did not enter Full Name<br>";
				}
				if(!isset($_POST['sltSex'])||$_POST['sltSex']=="-1" || $_POST['sltSex']==null)
				{
					$warning .="You did not enter Gender<br>";
				}
				if(!isset($_POST['email'])||$_POST['email']=="" || $_POST['email']==null)
				{
					$warning .="You did not enter Email<br>";
				}
				else if(preg_match($pattern, $_POST['email'])==0) 
				{
					$warning .="Enter your Email Wrong Structure<br>";
				}
				else 
				{
					if(isset($_GET['id']) && is_numeric($_GET['id']))
					{
						$temp = $tbUser->selectUserByEmail($_POST['email'],$_GET['id']);
					}
					else
					{
						$temp = $tbUser->selectUserByEmail($_POST['email'],0);
					}
					if(count($temp)>0)
					{
						$warning .="Email was in use<br>";
					}
				}
				if(!isset($_POST['address'])||$_POST['address']=="" || $_POST['address']==null)
				{
					$warning .="You did not enter Address<br>";
				}
				
				
				if(isset($_GET['id']) && is_numeric($_GET['id']))
				{
					if(isset($_POST['password'])&& $_POST['password']!="" && $_POST['password'] != $_POST['confirm'])
					{
						$warning .="Password and Confirm Password do not match<br>";
					}
					
					if($warning=="")
					{
						if($_POST['password']=="")
						{
							$pass= "";
						}
						else
						{
							$pass = md5($_POST['password']);
						}
							$array = array(
							"user_id" => $_GET['id'],
							"user_name" => $_POST['username'],
							"user_code" => $_POST['username'],
							"full_name" => $_POST['fullname'],
							"sex" => $_POST['sltSex'],
							"birth_day" => $date->change_date_to_int($_POST['mBirthday'], $_POST['dBirthday'], $_POST['yBirthday']),
							"address" => $_POST['address'],
							"career" => $_POST['work'],
							"email" => $_POST['email'],
							"updated_date" => time(),
							"group_id" => $_POST['user_group_id'],
							"status" => $_POST['status'],
							"password" => $pass,
							);
							$tbUser->updateUser($array);
							$session->update_user = "Updated success";
							Header("Location: /admin/user" );
					}
					else 
					{
						$this->view->data_user = $_POST;
					}
			
				}
				else
				{
					if(!isset($_POST['password'])||$_POST['password']=="" || $_POST['password']==null)
					{
						$warning .="Bạn Chưa Nhập Password<br>";
					}
					if(isset($_POST['password'])&& $_POST['password']!="" && $_POST['password'] != $_POST['confirm'])
					{
						$warning .="Password và Confirm Password do not match<br>";
					}
					
					if($warning=="") 
					{
						
						$array = array(
						"user_name" => $_POST['username'],
						"user_code" => $_POST['username'],
						"full_name" => $_POST['fullname'],
						"sex" => $_POST['sltSex'],
						"birth_day" => $date->change_date_to_int($_POST['mBirthday'], $_POST['dBirthday'], $_POST['yBirthday']),
						"address" => $_POST['address'],
						"career" => $_POST['work'],
						"email" => $_POST['email'],
						"created_date" => time(),
						"updated_date" => time(),
						"group_id" => $_POST['user_group_id'],
						"status" => $_POST['status'],
						"avatar" => "/images/avatar.jpeg",
						"password" => md5($_POST['password']),
						);
	
						$tbUser->insertUser($array);
						
						$session->insert_user = "Inserted success";
		  				Header( "Location: /admin/user" );
					}
					else 
					{
						$this->view->data_user = $_POST;
					}
					
				}
				$this->view->warning = $warning;
			
		}
		else if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
				$user_user_info = $tbUser->selectUserByUserID($_GET['id']);
				
				$array = array(
					"username" => $user_user_info[0]['user_name'],
					"fullname" => $user_user_info[0]['full_name'],
					"sltSex" => $user_user_info[0]['sex'],
					"mBirthday" => date("m",$user_user_info[0]['birth_day']),
					"dBirthday" => date("d",$user_user_info[0]['birth_day']),
					"yBirthday" =>date("Y",$user_user_info[0]['birth_day']),
					"address" => $user_user_info[0]['address'],
					"work" => $user_user_info[0]['career'],
					"email" => $user_user_info[0]['email'],
					"user_group_id" => $user_user_info[0]['group_id'],
					"status" => $user_user_info[0]['status'],
					);
				$this->view->data_user = $array;
		}
		
	}
	public function deleteAction()
	{
		$session = new Zend_Session_Namespace('login.auth');
		$tbUser = new Admin_Model_Users();
		if(isset($_POST['selected']) && $_POST['selected']!=null) {
			foreach($_POST['selected'] as $id)
			{
				$tbUser->deleteUser($id);
			}
		}
		
	  $session->delete_user = "Deleted success";	   
	  Header( "Location: /admin/user" );
	}
}